/************************************************************************************/
/*  Program name CAPAPEXT_PAY_FORMAT.sql                                            */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
set verify off;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPEXT_PAY_FORMAT';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName1	VARCHAR2(100)	:=	'CAPAP_PAY_FORMAT.dat';
	lv_FileName2	VARCHAR2(100)	:=	'CAPAP_PAY_PROGRAMS.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO			VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	CURSOR Cur_T(p_NGO VARCHAR2) IS
	SELECT
		F.*,
		P1.Program_Name		Build_Pgm_Name,
		P1.Friendly_Name	Build_Name,
		P2.Program_Name		Format_Pgm_Name,
		P2.Friendly_Name	Format_Name,
		P3.Program_Name		Remit_Pgm_Name,
		P3.Friendly_Name	Remit_Name
	FROM
		AP_CHECK_FORMATS	F,
		AP_PAYMENT_PROGRAMS	P1,
		AP_PAYMENT_PROGRAMS	P2,
		AP_PAYMENT_PROGRAMS	P3
	WHERE	SUBSTR(F.Name,1,4)				=	p_NGO
	AND		F.Build_Payments_Program_ID		=	P1.Program_ID(+)
	AND		F.Format_Payments_Program_ID	=	P2.Program_ID(+)
	AND		F.Remittance_Advice_Program_ID	=	P3.Program_ID(+);

	CURSOR Cur_P(p_NGO VARCHAR2) IS
	SELECT
		P.*
	FROM
		AP_PAYMENT_PROGRAMS p
	WHERE	SUBSTR(P.Friendly_Name,1,4) = p_NGO;

BEGIN
	lv_NGO			:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	SUBSTR(Name,4,4)	=	lv_NGO;

	IF	lv_NGO_Count	>=	1	THEN
		lv_Err_Msg		:=	'No Error';
		lv_Scc_Count	:=	0;
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName1, 'w');
		FOR C1 IN Cur_T(lv_NGO) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.Check_Format_ID||CHR(9)||
				C1.Last_Update_Date||CHR(9)||
					C1.Last_Updated_By||CHR(9)||
				C1.Name||CHR(9)||
				C1.Type||CHR(9)||
				C1.Stub_First_Flag||CHR(9)||
				C1.Check_Length||CHR(9)||
				C1.Stub_Length||CHR(9)||
				C1.Invoices_Per_Stub||CHR(9)||
				C1.Overflow_Rule||CHR(9)||
				C1.Pre_Numbered_Flag||CHR(9)||
				C1.Check_Num_Row_Line||CHR(9)||
				C1.Date_Row_Line||CHR(9)||
				C1.Amount_Row_Line||CHR(9)||
				C1.Amount_Words_Row_Line||CHR(9)||
				C1.Vendor_Num_Row_Line||CHR(9)||
				C1.Vendor_Name_Row_Line||CHR(9)||
				C1.Vendor_Address_Row_Line||CHR(9)||
				C1.Stub_Check_Num_Row_Line||CHR(9)||
				C1.Stub_Vendor_Name_Row_Line||CHR(9)||
				C1.Stub_Vendor_Num_Row_Line||CHR(9)||
				C1.Stub_First_Invoice_Row_Line||CHR(9)||
				C1.Payment_Method_Lookup_Code||CHR(9)||
				C1.Currency_Code||CHR(9)||
				C1.Create_Payments_Program_ID||CHR(9)||
				C1.Confirm_Payments_Program_ID||CHR(9)||
				C1.Separate_Remittance_Advice||CHR(9)||
					C1.Remittance_Advice_Program_ID||CHR(9)||
				C1.Last_Update_Login||CHR(9)||
				C1.Creation_Date||CHR(9)||
					C1.Created_By||CHR(9)||
				C1.EFT_Type||CHR(9)||
				C1.Multi_Currency_Flag||CHR(9)||
				C1.Attribute_Category||CHR(9)||
				C1.Attribute1||CHR(9)||
				C1.Attribute2||CHR(9)||
				C1.Attribute3||CHR(9)||
				C1.Attribute4||CHR(9)||
				C1.Attribute5||CHR(9)||
				C1.Attribute6||CHR(9)||
				C1.Attribute7||CHR(9)||
				C1.Attribute8||CHR(9)||
				C1.Attribute9||CHR(9)||
				C1.Attribute10||CHR(9)||
				C1.Attribute11||CHR(9)||
				C1.Attribute12||CHR(9)||
				C1.Attribute13||CHR(9)||
				C1.Attribute14||CHR(9)||
				C1.Attribute15||CHR(9)||
				C1.Zero_Amounts_Only||CHR(9)||
					C1.Build_Payments_Program_ID||CHR(9)||
					C1.Format_Payments_Program_ID||CHR(9)||
				C1.Print_Check_Stub||CHR(9)||
				C1.Group_By_Due_Date||CHR(9)||
				C1.Global_Attribute_Category||CHR(9)||
				C1.Global_Attribute1||CHR(9)||
				C1.Global_Attribute2||CHR(9)||
				C1.Global_Attribute3||CHR(9)||
				C1.Global_Attribute4||CHR(9)||
				C1.Global_Attribute5||CHR(9)||
				C1.Global_Attribute6||CHR(9)||
				C1.Global_Attribute7||CHR(9)||
				C1.Global_Attribute8||CHR(9)||
				C1.Global_Attribute9||CHR(9)||
				C1.Global_Attribute10||CHR(9)||
				C1.Global_Attribute11||CHR(9)||
				C1.Global_Attribute12||CHR(9)||
				C1.Global_Attribute13||CHR(9)||
				C1.Global_Attribute14||CHR(9)||
				C1.Global_Attribute15||CHR(9)||
				C1.Global_Attribute16||CHR(9)||
				C1.Global_Attribute17||CHR(9)||
				C1.Global_Attribute18||CHR(9)||
				C1.Global_Attribute19||CHR(9)||
				C1.Global_Attribute20||CHR(9)||
				C1.Bank_File_Character_Set||CHR(9)||
				C1.Transmissions_Flag||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Last_Updated_By)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Created_By)||CHR(9)||
				C1.Build_Pgm_Name||CHR(9)||
				C1.Build_Name||CHR(9)||
				C1.Format_Pgm_Name||CHR(9)||
				C1.Format_Name||CHR(9)||
				C1.Remit_Pgm_Name||CHR(9)||
				C1.Remit_Name
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);

		IF	lv_Scc_Count	=	0	THEN
			lv_Err_Msg		:=	lv_NGO||': No payment formats found for this NGO';
		END IF;
		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID||' CAPAP_PAY_FORMAT',
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
		lv_Err_Msg		:=	'No Error';
		lv_Scc_Count	:=	0;
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName2, 'w');
		FOR C2 IN Cur_P(lv_NGO) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C2.Program_ID||CHR(9)||
				C2.Program_Name||CHR(9)||
				C2.Program_Type||CHR(9)||
				C2.Friendly_Name||CHR(9)||
				C2.Last_Update_Date||CHR(9)||
					C2.Last_Updated_By||CHR(9)||
				C2.Creation_Date||CHR(9)||
					C2.Created_By||CHR(9)||
				C2.Last_Update_Login||CHR(9)||
				C2.Transmissions_Enabled||CHR(9)||
				CAPBREXT_COMMON.GetUser(C2.Last_Updated_By)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C2.Created_By)
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);

		IF	lv_Scc_Count	=	0	THEN
			lv_Err_Msg		:=	lv_NGO||': No payment programs found for this NGO or CAP';
		END IF;
		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID||' CAPAP_PAY_PROGRAMS',
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO||': No such NGO exists';
		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID,
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
	END IF;

	COMMIT;
END;
/